/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.dao.ResourcesMapper;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.Resources;
import com.rf.sys.model.ResourcesExample;
import com.rf.sys.service.ResourcesService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Service("resourcesService")
public class ResourcesServiceImpl implements ResourcesService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private ResourcesMapper resourcesMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param resourcesId
	 * @return Resources
	 */
   	@Override
	public Resources getResourcesByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return resourcesMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据Resources获取数据
	 * @param Resources
	 * @return List<Resources>
	 */
	@Override
	public List<Resources> getResources(Resources resources) throws Exception{
		// TODO Auto-generated method stub
		ResourcesExample example = new ResourcesExample();
		List<Resources> list = resourcesMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Resources>
	 */
	@Override
	public List<Resources> getResources() throws Exception{
		// TODO Auto-generated method stub
		ResourcesExample example = new ResourcesExample();
		example.createCriteria().andStateNotEqualTo(ConfigUtils.DELSTATE);
		return resourcesMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Resources
	 * @return
	 */
	@Override
	public void addResources(Resources resources) throws Exception{
		// TODO Auto-generated method stub
		resources.setId(UuidUtil.get32UUID());
		resourcesMapper.insert(resources);
	}

	/**
	 * 修改数据
	 * @param Resources
	 * @param
	 */
	@Override
	public void updateResources(Resources resources) throws Exception{
		// TODO Auto-generated method stub
		resourcesMapper.updateByPrimaryKeySelective(resources);
	}
	
	/**
	 * 删除数据
	 * @param resourcesId
	 * @param
	 */
	@Override
	public void deleteResources(String id) throws Exception{
		// TODO Auto-generated method stub
		resourcesMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param Resources
	 * @param limit
	 * @param offset
	 * @return Map
	 */
	@Override
	public Map getResourcesPage(Resources resources,PageUtils pageUtils) throws Exception{
		ResourcesExample example = new ResourcesExample();
		com.rf.sys.model.ResourcesExample.Criteria criteria= example.createCriteria();
		
		criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);
		//获取第page页，每页共rows条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Resources> list = resourcesMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	
	/**
	 * 根据用户ID获取数据--登陆后资源加载
	 * @param userId
	 * @return List<Map<String, Object>>
	 */
	@Override
	public List<Map<String, Object>> getResourcesByUserId(String userId) throws Exception{
		// TODO Auto-generated method stub
		String sql = "select DISTINCT r.id,r.name,r.parent_id parentId,r.res_url resUrl,r.res_type resType, "
			+ " r.res_level resLevel,r.description,r.icon,r.sort,r.state "
			+ " from sys_resources r,sys_role_res rr,sys_user_role ur,sys_user u,sys_role ro  "
			+ " where r.id = rr.res_id and rr.role_id = ur.role_id and rr.role_id = ro.id and ur.user_id = u.id "
			+ " and u.id = '"+userId+"' and r.state = '100101' and ro.state ='100101' ORDER BY  r.sort  ";
		List<Map<String, Object>> list = sqlAdapterMapper.selectSQL(sql);
		return list;
	}
}

